package cn.glow.readingcloud.homepage.service.impl;


import cn.glow.readingcloud.common.result.Result;
import cn.glow.readingcloud.common.result.ResultUtil;
import cn.glow.readingcloud.common.utils.CommonUtil;
import cn.glow.readingcloud.homepage.dao.IndexPageConfigMapper;
import cn.glow.readingcloud.homepage.dto.IndexPageView;
import cn.glow.readingcloud.homepage.pojo.IndexPageConfig;
import cn.glow.readingcloud.homepage.service.IndexBannerService;
import cn.glow.readingcloud.homepage.service.IndexBooklistService;
import cn.glow.readingcloud.homepage.service.IndexPageConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Glow
 * @Date 2023-04-21 15:36:27
 * @Description
 * @Version 1.0
 */
@Service
public class IndexPageConfigServiceImpl extends ServiceImpl<IndexPageConfigMapper, IndexPageConfig> implements IndexPageConfigService {
    private static final Logger LOGGER = LoggerFactory.getLogger(IndexPageConfigServiceImpl.class);
    @Autowired
    private IndexPageConfigMapper indexPageConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IndexBooklistService indexBooklistService;
    @Autowired
    private IndexBannerService indexBannerService;
    @Override
    public Result getIndexPageByType(Integer type, Integer page, Integer limit)
    {

        // 精品页VO列表
        List<IndexPageView> pageVOS =(List<IndexPageView>)redisTemplate.opsForHash().get("homepage-index-"+type, page.toString());
        if (pageVOS != null) {
            return ResultUtil.success(pageVOS);
        }

        // 获得精品页配置
        List<IndexPageConfig> pageConfigs = this.getIndexPageWithPaging(type, page, limit);
        if (CommonUtil.isEmpty(pageConfigs)) {
            LOGGER.warn("当前请求页没有配置项！");
            return ResultUtil.success(new ArrayList<>()).buildMessage("当前请求页没有配置项！");
        }

        pageVOS = new ArrayList<>(pageConfigs.size());
        for (int i = 0; i < pageConfigs.size(); i++) {
            IndexPageConfig pageConfig = pageConfigs.get(i);
            IndexPageView vo = new IndexPageView();
            BeanUtils.copyProperties(pageConfig, vo);

            // 模块是否有效
            boolean okFlag = true;
            switch (pageConfig.getItemType()){
                case 1:
                    // 书单
                    vo.setBooklist(this.indexBooklistService.getIndexBooklistVO(pageConfig.getItemId(), null));
                    if (vo.getBooklist() == null) {
                        okFlag = false;
                    }
                    break;
                case 2:
                    // Banner
                    vo.setBanner(indexBannerService.getBannerVO(pageConfig.getItemId()));
                    if (vo.getBanner() == null) {
                        okFlag = false;
                    }
                    break;
                default:
                    break;
            }

            // 对应的模块值不为空，才进行添加到VO中
            if (okFlag) {
                pageVOS.add(vo);
            }
        }

        if (pageVOS.size() > 0) {
            // 缓存精品页
            redisTemplate.opsForHash().put("homepage-index-"+type, page.toString(),pageVOS);
        }
        return ResultUtil.success(pageVOS);
    }

    /**
     * 分页获取精品页配置列表
     * @param type
     * @param page
     * @param limit
     * @return
     */
    private List<IndexPageConfig> getIndexPageWithPaging(Integer type, Integer page, Integer limit){
        if (page <= 0) {
            page = 1;
        }
        Page<IndexPageConfig> pages = new Page<>(page,limit);
        LambdaQueryWrapper<IndexPageConfig>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IndexPageConfig::getPageType,type);

        List<IndexPageConfig> pageWithResult = indexPageConfigMapper.selectPage(pages,queryWrapper).getRecords();
        return pageWithResult;
    }
}
